<script setup>
import { ref } from 'vue'
import { showToast } from 'vant'
import {useRoute,useRouter} from 'vue-router'
import {post}from '../../utils/request'
const route = useRoute()
const router=useRouter()    
const password = ref('')
const showKeyboard = ref(false)

const handleInputClick = () => {
  if (!showKeyboard.value) {
    showKeyboard.value = true
  }
}

const onInput = (val) => {
  if (password.value.length < 4) {
    password.value += val
  }
}

const onDelete = () => {
  password.value = password.value.slice(0, -1)
}

const onSubmit = () => {
  if (password.value.length < 4) {
    showToast('请输入4位支付密码')
    return
  }else{
    console.log(password.value);
    post('/wyq/chguser2',{
      zfpwd:password.value,
      username:route.query.username
    }).then(res=>{
      if(res.data.code==200){
        showToast({
            message:res.data.msg,
            onClose:()=>[
                router.push('/login')
            ]
        })
      }
    })
  }
}

const onBack = () => {
  // 返回逻辑
}
</script>

<template>
    <div class="container">
      <van-nav-bar
        title="设置支付密码"
        left-arrow
        @click-left="onBack"
      />
  
      <div class="body">
        <div class="text">
          <p>为保证您的资金安全</p>
          <p>请设置单独的支付密码</p>
        </div>
  
        <div class="input-box" @click="handleInputClick">
          <div
            v-for="(item, index) in 4"
            :key="index"
            class="cell"
            :class="{ active: index === password.length }"
          >
            <span v-if="index < password.length">
              {{ index === password.length - 1 ? password[index] : '●' }}
            </span>
          </div>
        </div>
      </div>
  
      <div class="bottom-area">
        <van-button
          type="primary"
          block
          class="submit-btn"
          @click="onSubmit"
        >
          继续
        </van-button>
  
        <van-number-keyboard
          :show="showKeyboard"
          theme="custom"
          maxlength="4"
          @input="onInput"
          @delete="onDelete"
          @blur="showKeyboard = false"
        />
      </div>
    </div>
  </template>
  

  
  <style scoped>
  .container {
    height: 100vh;
    background: #fff;
    display: flex;
    flex-direction: column;
  }
  
  .body {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    padding-top: 48px;
  }
  
  .text {
    text-align: center;
    font-size: 14px;
    color: #333;
    line-height: 22px;
    margin-bottom: 40px;
  }
  
  .input-box {
    display: flex;
    justify-content: space-between;
    width: 240px;
    height: 56px;
    cursor: pointer;
  }
  
  .cell {
    width: 56px;
    height: 56px;
    border: 1px solid #ccc;
    border-radius: 12px;
    text-align: center;
    font-size: 22px;
    line-height: 56px;
    box-sizing: border-box;
  }
  
  .active {
    border-color: #000;
  }
  
  .bottom-area {
    padding: 0 16px 16px;
  }
  
  .submit-btn {
    margin-bottom: 12px;
    height: 48px;
    border-radius: 24px;
    background-color: #000;
    color: #fff;
    font-size: 16px;
  }
  </style>
  